home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / demos / GL / snurb / data.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  6.7 KB  |  329 lines

  1. /*
  2.  * Copyright 1991, 1992, 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. /* data.c */
  18.  
  19. #include <gl.h>
  20. #include <stdio.h>
  21. #include <malloc.h>
  22. #include "defines.h"
  23. #include "data.h"
  24.  
  25.  
  26. /* The root of the whole scene */
  27. struct node_struct *root;
  28.  
  29. static void free_children(struct node_struct *child);
  30.  
  31. void init_data(void)
  32. {
  33.     root = make_new_node();
  34.  
  35.     root->node_type = ROOT;
  36. }
  37.  
  38.  
  39.  
  40. /* Allocates and returns a pointer to a new patch structure */
  41. struct patch_struct *make_new_patch(void)
  42. {
  43.     return (struct patch_struct *) malloc(sizeof(struct patch_struct));
  44. }
  45.     
  46.  
  47.  
  48. /* Allocate memory for a new object node and return a pointer to it.
  49.  * Initializes all structure elements to NULL/FALSE.
  50.  */
  51. struct node_struct *make_new_node(void)
  52. {
  53.     struct node_struct *new_node;
  54.  
  55.     new_node = (struct node_struct *) malloc(sizeof(struct node_struct));
  56.  
  57.     new_node->node_type = OBJECT;
  58.     new_node->parent = NULL;
  59.     new_node->child = NULL;
  60.     new_node->sibling = NULL;
  61.     new_node->prev_sibling = NULL;
  62.     new_node->patch = NULL;
  63.     new_node->selected = FALSE;
  64.  
  65.     return new_node;
  66. }
  67.  
  68.  
  69. struct zip_struct *make_new_zipper(void)
  70. {
  71.     return (struct zip_struct *) malloc(sizeof(struct zip_struct));
  72. }
  73.     
  74.  
  75. struct node_struct *make_new_patch_node()
  76. {
  77.     int i;
  78.     struct node_struct *new_object;
  79.  
  80.     new_object = make_new_node();
  81.     new_object->node_type = PATCH;
  82.     new_object->patch = make_new_patch();
  83.  
  84.     for (i=0; i<4; i++)
  85.     {
  86.         new_object->patch->zipper[i] = make_new_zipper();
  87.         new_object->patch->zipper[i]->zipped = FALSE;
  88.         new_object->patch->zipper[i]->zip_type = NONE;
  89.         new_object->patch->zipper[i]->patch = NULL;
  90.         new_object->patch->zipper[i]->edge = NONE;
  91.         
  92.         new_object->patch->zipper[i]->s_index = NONE;
  93.         new_object->patch->zipper[i]->s_step = 0;
  94.         new_object->patch->zipper[i]->t_index = NONE;
  95.         new_object->patch->zipper[i]->t_step = 0;
  96.         
  97.         new_object->patch->zipper[i]->i_s_index = NONE;
  98.         new_object->patch->zipper[i]->i_s_step = 0;
  99.         new_object->patch->zipper[i]->i_t_index = NONE;
  100.         new_object->patch->zipper[i]->i_t_step = 0;
  101.     }
  102.             
  103.  
  104.     
  105.     return new_object;
  106. }
  107.  
  108.  
  109.  
  110.                 
  111.  
  112. /* Attaches new_node as the last child of parent */
  113. void attach_node_to_parent(
  114.     struct node_struct *new_node, 
  115.     struct node_struct *parent)
  116. {
  117.     struct node_struct *last_child;
  118.  
  119.     new_node->parent = parent;
  120.     new_node->sibling = NULL;
  121.     
  122.     if (parent->child == NULL)
  123.     {
  124.         new_node->prev_sibling = NULL;
  125.         parent->child = new_node;
  126.     }
  127.     else
  128.     {
  129.         last_child = find_last_sibling(parent->child);
  130.         last_child->sibling = new_node;
  131.         new_node->prev_sibling = last_child;
  132.     }
  133. }
  134.  
  135.  
  136.  
  137. struct node_struct *find_last_sibling(struct node_struct *older_sibling)
  138. {
  139.     if (older_sibling->sibling == NULL)
  140.         return(older_sibling);
  141.     else
  142.         find_last_sibling(older_sibling->sibling);
  143. }
  144.  
  145.  
  146.  
  147. /* frees the child node, as well as all its children and siblings */
  148. static void free_children(struct node_struct *child)
  149. {
  150.     if (child != NULL)
  151.     {
  152.         if ((child->child != NULL) || (child->sibling != NULL))
  153.         {
  154.             free_children(child->child);
  155.             free_children(child->sibling);
  156.         }
  157.         
  158.         if (child->node_type == PATCH)
  159.         {
  160.             free((char *) child->patch->zipper[0]);
  161.             free((char *) child->patch->zipper[1]);
  162.             free((char *) child->patch->zipper[2]);
  163.             free((char *) child->patch->zipper[3]);
  164.             free((char *) child->patch);
  165.         }
  166.  
  167.         free((char *) child);
  168.     }
  169. }
  170.  
  171.  
  172. /* deletes an object and all its children, releasing the memory they occupied */
  173. void free_object(struct node_struct *object)
  174. {
  175.     if (object != NULL)
  176.     {
  177.         if (object->node_type != PATCH)
  178.             free_children(object->child);
  179.         else if (object->node_type == PATCH)
  180.         {
  181.             unzip_all(object);
  182.             free((char *) object->patch);
  183.         }
  184.                     
  185.         if (object->prev_sibling != NULL)
  186.             object->prev_sibling->sibling = object->sibling;
  187.         else
  188.             object->parent->child = object->sibling;
  189.  
  190.         if (object->sibling != NULL)
  191.             object->sibling->prev_sibling = object->prev_sibling;
  192.  
  193.             
  194.  
  195.         free((char *) object);
  196.     }
  197. }
  198.  
  199.  
  200.  
  201. static reattach_selected_children(
  202.     struct node_struct *child,
  203.     struct node_struct *parent)
  204. {
  205.     struct node_struct *next_child;
  206.     
  207.     if (child != NULL)
  208.     {
  209.         next_child = child->sibling;
  210.         
  211.         if (child->selected)
  212.         {
  213.             if (child->prev_sibling == NULL)
  214.                 child->parent->child = child->sibling;
  215.             else
  216.                 child->prev_sibling->sibling = child->sibling;
  217.  
  218.             if (child->sibling != NULL)
  219.                 child->sibling->prev_sibling = child->prev_sibling;
  220.  
  221.             child->selected = FALSE;
  222.             
  223.             attach_node_to_parent(child, parent);
  224.         }
  225.         
  226.         reattach_selected_children(next_child, parent);
  227.     }
  228. }
  229.  
  230.     
  231.  
  232.  
  233. /* take those children which have their selected flag set and attach under
  234.  * a new parent node, and attch that new node to parent.
  235.  */
  236. void group_selected_children(struct node_struct *parent)
  237. {
  238.     struct node_struct *new_object;
  239.  
  240.     new_object = make_new_node();
  241.  
  242.     reattach_selected_children(parent->child, new_object);
  243.  
  244.     attach_node_to_parent(new_object, parent);
  245.  
  246.     new_object->selected = TRUE;
  247. }
  248.  
  249.  
  250. static void reassign_parent(
  251.     struct node_struct *child,
  252.     struct node_struct *parent)
  253. {
  254.     if (child != NULL)
  255.     {
  256.         child->parent = parent;
  257.         reassign_parent(child->sibling, parent);
  258.     }
  259. }
  260.  
  261. static void attach_grandchildren_to_parent(
  262.     struct node_struct *grandchild,
  263.     struct node_struct *parent)
  264. {
  265.     struct node_struct *last_child;
  266.     
  267.     if (grandchild != NULL)
  268.     {
  269.         last_child = find_last_sibling(parent->child);
  270.  
  271.         last_child->sibling = grandchild;
  272.         grandchild->prev_sibling = last_child;
  273.  
  274.         reassign_parent(grandchild, parent);
  275.     }
  276. }
  277.  
  278.     
  279.  
  280.  
  281.  
  282.  
  283. /* for each object node (not patch node) that is selected, detach it
  284.  * and reattach it to the child's parent. Then remove the child.
  285.  */
  286. void ungroup_selected_children(struct node_struct *child)
  287. {
  288.     struct node_struct *next_child;
  289.     
  290.     if (child != NULL)
  291.     {
  292.         next_child = child->sibling;
  293.         
  294.         if ((child->selected) && (child->node_type == OBJECT))
  295.         {
  296.             attach_grandchildren_to_parent(child->child,child->parent);
  297.             child->child = NULL;
  298.  
  299.             free_object(child);
  300.         }
  301.  
  302.         ungroup_selected_children(next_child);
  303.     }
  304. }
  305.  
  306.  
  307.  
  308.  
  309. struct node_struct *find_rootmost_parent(struct node_struct *child)
  310. {
  311.     if (child != NULL)
  312.     {
  313.         if (child->parent == root)
  314.             return child;
  315.         else
  316.             return( find_rootmost_parent(child->parent));
  317.     }
  318.     else
  319.     {
  320.         fprintf(stderr, "Uh Oh. Got a null node in find_rootmost_parent\n");
  321.         exit(0);
  322.     }
  323. }
  324.  
  325.  
  326.  
  327.  
  328.  
  329.